package com.superlistaportable.sales;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import twitter4j.HashtagEntity;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
import android.content.Intent;
import android.os.Bundle;

public class SalesModel {
	String selectedSupermarket;
	public SalesModel(Intent i){
		Bundle extras = i.getExtras();
		if (extras != null) {
			selectedSupermarket = extras.getString("selectedSupermarket");
        }
    }
	
	public List<Status> getNewStatuses() {
		Twitter twitter = getTwitter();
		
	    List<Status> statuses = new ArrayList<Status>();
		try {
			statuses = twitter.getHomeTimeline();
		} catch (TwitterException e) {
			return statuses;
		}
		
		List<String> filters = Arrays.asList(selectedSupermarket,"ofertas");
		List<Status> supermarketSalesStatuses = new ArrayList<Status>();
		List<String> entitiesText = new ArrayList<String>();
		boolean isOk = true;
		
		for(Status status : statuses){  
			HashtagEntity[] entitiesArray = status.getHashtagEntities();
			for (HashtagEntity entity : entitiesArray ){
				entitiesText.add(entity.getText());
			}
			
			for(String filter : filters){
                if(!entitiesText.contains(filter)){
                    isOk = false & isOk;
                 }
			}
             if(isOk){
            	 supermarketSalesStatuses.add(status);
             	}
        	 entitiesText.clear();
        	 isOk = true;
			}
		
		return supermarketSalesStatuses;
	}
	
	
	private Twitter getTwitter() {
		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true)
		  .setOAuthConsumerKey("VcsysrTCn5Ut0OyFduzoAg")
		  .setOAuthConsumerSecret("mYkTvK6H6tXb1fZKF8unOWpvWY0vpb3HkD6KfWYJG3A")
		  .setOAuthAccessToken("1928284668-NAhJ1dgjXzxzeJVRChYcHsDklVLTNTbxoBOetC0")
		  .setOAuthAccessTokenSecret("KSAQ8E2Qeq25JypVpHCyS0bHCZMyLbLd1POEiRmcTWw");
		TwitterFactory tf = new TwitterFactory(cb.build());
		Twitter twitter = tf.getInstance();
		return twitter;
	}
}
